using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace Pixysoft.Data
{
    public class TableMapping
    {
        private static TableMapping instance;
        public static TableMapping Instance
        {
            get
            {
                lock (typeof(TableMapping))
                {
                    if (instance == null)
                        instance = new TableMapping();
                    return instance;
                }
            }
        }

        public void MappingOneToOne(DataRow fromRow, DataRow toRow)
        {
            if (fromRow == null || toRow == null)
                throw new Exception("table is null");

            DataTable toTable = toRow.Table;

            foreach (DataColumn fromColumn in fromRow.Table.Columns)
            {
                if (!toTable.Columns.Contains(fromColumn.ColumnName))
                    continue;

                if (toRow[fromColumn.ColumnName] == null || toRow[fromColumn.ColumnName] == DBNull.Value)
                    toRow[fromColumn.ColumnName] = fromRow[fromColumn];
            }
        }

        public void MappingManyToMany(DataTable fromTable, DataTable toTable)
        {
            if (fromTable == null || toTable == null)
                throw new Exception("table is null");

            int counter = 0;

            foreach (DataRow fromRow in fromTable.Rows)
            {
                if (counter > toTable.Rows.Count - 1)
                {
                    toTable.Rows.Add(toTable.NewRow());
                }

                DataRow toRow = toTable.Rows[counter];

                MappingOneToOne(fromRow, toRow);

                counter++;
            }
        }

        public void StrongMappingOneToOne(DataRow fromRow, DataRow toRow)
        {
            if (fromRow == null || toRow == null)
                throw new Exception("table is null");

            DataTable toTable = toRow.Table;

            foreach (DataColumn fromColumn in fromRow.Table.Columns)
            {
                if (!toTable.Columns.Contains(fromColumn.ColumnName))
                    continue;

                toRow[fromColumn.ColumnName] = fromRow[fromColumn];
            }
        }

        public void StrongMappingManyToMany(DataTable fromTable, DataTable toTable)
        {
            if (fromTable == null || toTable == null)
                throw new Exception("table is null");

            int counter = 0;

            foreach (DataRow fromRow in fromTable.Rows)
            {
                if (counter > toTable.Rows.Count - 1)
                {
                    toTable.Rows.Add(toTable.NewRow());
                }

                DataRow toRow = toTable.Rows[counter];

                StrongMappingOneToOne(fromRow, toRow);

                counter++;
            }
        }

        public void IncMappingManyToMany(DataTable fromTable, DataTable toTable)
        {
            if (fromTable == null || toTable == null)
                throw new Exception("table is null");

            foreach (DataRow fromRow in fromTable.Rows)
            {
                DataRow toRow = toTable.NewRow();

                MappingOneToOne(fromRow, toRow);

                toTable.Rows.Add(toRow);
            }
        }

        private void test()
        {
            DataTable t1 = new DataTable("t");
            t1.Columns.Add("c1");
            DataRow t1row = t1.NewRow();
            t1row[0] = "hello";
            t1.Rows.Add(t1row);

            DataTable t2 = t1.Clone();
            Pixysoft.Data.TableMapping.Instance.IncMappingManyToMany(t1, t2);
            Pixysoft.Data.TableMapping.Instance.IncMappingManyToMany(t1, t2);
            Debug.SmartConsole.Write(t2);
        }

    }
}
